package com.ndp.fb.fbagent.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ndp.fb.exception.ErrorCode;
import com.ndp.fb.exception.VideoUploadException;
import com.ndp.fb.fbagent.service.AdResouceFBService;
import com.ndp.fb.model.business.bo.nfs.NfsConfig;
import com.ndp.fb.model.fbagent.param.adimage.AdImageParam;
import com.ndp.fb.model.fbagent.param.advideo.AdVideoParam;
import com.ndp.fb.redis.api.RedisClient;
import com.ndp.fb.util.NFSUtil;
import com.restfb.BinaryAttachment;
import com.restfb.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/4/9.
 */
@Service
public class AdResouceFBServiceImpl extends AbstractFBagentService implements AdResouceFBService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private NfsConfig nfsConfig;

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private NFSUtil nfsUtil;


    public void uploadAdImages(AdImageParam requestParam) {
        try {
            Map map = this.adapterMap(requestParam);
            List<BinaryAttachment> binaryAttachments = (List<BinaryAttachment>) map.get("binaryAttachments");
            Parameter[] parameters = (Parameter[]) map.get("parameters");
            String uri = "/act_" + requestParam.getFbAccountId() + "/adimages";
            this.makePostRequest(uri, requestParam.getAccessToken(), binaryAttachments, parameters);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }


    @Override
    public void startUploadAdVideo(AdVideoParam requestParam) throws  Exception {
            logger.info("开始发起视频上传请求:" + JSON.toJSONString(requestParam));
            Map<String, String> equipment = requestParam.getEquipment();
            equipment.put("upload_start_time", String.valueOf(System.currentTimeMillis()));
            List<Parameter> parameters = facebookParamAdapter.adapterParam(requestParam, false);
            String uri = "/act_" + requestParam.getFbAccountId() + "/advideos";
            List<BinaryAttachment> binaryAttachments = new ArrayList<>(1);
            binaryAttachments.add(null);
            this.makePostRequest(uri, requestParam.getAccessToken(), binaryAttachments, parameters.toArray(new Parameter[parameters.size()]));

    }

    @Override
    public void transferUploadAdVideo(AdVideoParam requestParam) throws  Exception {
            logger.info("进行视频上传:" + JSON.toJSONString(requestParam));
            requestParam.setUploadPhase("transfer");
            byte[] fileBytes = nfsUtil.downNfsFile(requestParam.getNfsId());
            if (fileBytes == null || fileBytes.length == 0) {
                logger.error("download Nfs is null! nfsId >>>>" + requestParam.getNfsId());
                throw new VideoUploadException(ErrorCode.ERROR_20012,requestParam.getNfsId());
            }
            List<Parameter> parameters = facebookParamAdapter.adapterParam(requestParam, false);
            BinaryAttachment binaryAttachment = BinaryAttachment.with("video_file_chunk", fileBytes);
            List<BinaryAttachment> binaryAttachments = new ArrayList<>(1);
            binaryAttachments.add(binaryAttachment);
            String uri = "/act_" + requestParam.getFbAccountId() + "/advideos";
            this.makePostRequest(uri, requestParam.getAccessToken(), binaryAttachments, parameters.toArray(new Parameter[parameters.size()]));
    }

    @Override
    public void finishUploadAdVideo(AdVideoParam requestParam) throws  Exception {
            logger.info("结束视频上传请求:" + JSON.toJSONString(requestParam));
            requestParam.setUploadPhase("finish");
            List<Parameter> parameters = facebookParamAdapter.adapterParam(requestParam, false);
            String uri = "/act_" + requestParam.getFbAccountId() + "/advideos";
            List<BinaryAttachment> binaryAttachments = new ArrayList<>(1);
            binaryAttachments.add(null);
            this.makePostRequest(uri, requestParam.getAccessToken(), binaryAttachments, parameters.toArray(new Parameter[parameters.size()]));
    }

}
